var nextGreaterElement = function (nums1, nums2) {
    //console.log('debug');
    var ngMap = {};
    var stack = [];
    for (var i in nums2) {
        var num = nums2[i];
        while (stack.length > 0 && stack[stack.length - 1] < num) {
            ngMap[stack.pop()] = num;
        }
        stack.push(num);
    }
    // console.log(ngMap);
    var ret = [];
    for (var i in nums1) {
        ret.push(ngMap[nums1[i]] === undefined ? -1 : ngMap[nums1[i]]);
    }
    console.log(ret);
    return ret;
};
/**
 * 
 */
// nextGreaterElement([4,1,2],[1,3,4,2]);
nextGreaterElement([1, 3, 5, 2, 4], [6, 5, 4, 3, 2, 1, 7]);


var lastRemaining = function (n, m) {
    if (n == 1)
        return 0;
    var f = lastRemaining(n - 1, m);
    return (f + m) % n;
};

//console.log(lastRemaining(70860,116922));//正确的递归方式 但是js栈溢出了
var lastRemaining2 = function (n, m) {
    var f = 0;//通过递归反推出迭代
    for(var i=2; i<=n; i++){
        f = (f + m) %n;
    }
    return f;
};

